package org.csu.mypetstore.controller;


import com.sun.org.apache.xpath.internal.operations.Mod;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

@Controller
@RequestMapping("/catalog")
@SessionAttributes(value = {"account", "order", "cart","userInfo"})
public class CatalogController {

      @Autowired
      private CatalogService catalogService;

      @GetMapping("/view")
      public String view() {

            return "catalog/main";

      }

      @GetMapping("/viewCategory")
      public String viewCategory(String categoryId, Model model) {
            if (categoryId != null) {
                  Category category = catalogService.getCategory(categoryId);
                  List<Product> productList = catalogService.getProductListByCategory(categoryId);
                  model.addAttribute("category", category);
                  model.addAttribute("productList", productList);
                  return "catalog/category";
            } else
                  return "catalog/main";
      }

      @GetMapping("/viewProduct")
      public String viewProduct(String productId, Model model) {
            System.out.println("47"+productId);
            if (productId != null) {
                  Product product = catalogService.getProduct(productId);
                  List<Item> itemList = catalogService.getItemListByProduct(productId);
                  System.out.println(itemList.size());
                  System.out.println(itemList.get(0).getInStore());
                  model.addAttribute("product", product);
                  model.addAttribute("itemList", itemList);
                  return "catalog/product";
            } else
                  return "catalog/category";
//                    itemList = catalogService.getItemListByProduct(productId);
//                    product = catalogService.getProduct(productId);
      }

      @GetMapping("/viewItem")
      public String viewItem(String itemId, Model model) {
            if (itemId != null) {
                  Item item = catalogService.getItem(itemId);
                  Product product = item.getProduct();
                  model.addAttribute("product", product);
                  model.addAttribute("item", item);
                  return "catalog/item";
            } else
                  return "catalog/product";
      }

//      @GetMapping(/)
//      public String searchProducts() {
//            if (keyword == null || keyword.length() < 1) {
//                  setMessage("Please enter a keyword to search for, then press the search button.");
//                  return new ForwardResolution(ERROR);
//            } else {
//                  productList = catalogService.searchProductList(keyword.toLowerCase());
//                  return new ForwardResolution(SEARCH_PRODUCTS);
//            }
//      }


}
